package com.lanxin.jet.services.impl;

import java.util.List;

import com.lanxin.jet.beans.CommentBean;
import com.lanxin.jet.mappers.CommentMapper;
import com.lanxin.jet.services.CommentService;
import com.lanxin.jet.tools.JetCriteria;
import com.lanxin.jet.tools.JetDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Override
    public String insert(String content, String bid, String uid) {
        CommentBean comment = new CommentBean();
        comment.setId(JetDate.uniqid());
        comment.setBid(bid);
        comment.setUid(uid);
        comment.setContent(content);
        comment.setCreateTime(JetDate.timeNow());
        comment.setStatus(STATUS_NORMAL);
        commentMapper.insert(comment);
        return comment.getId();
    }

    @Override
    public int count(String bid, int status, String keyword) {
        JetCriteria criteria = new JetCriteria();
        criteria.add("bid", bid);
        if (status != -1) {
            criteria.add("status", status);
        }
        criteria.addLike("content", keyword);
        return commentMapper.count(criteria);
    }

    @Override
    public CommentBean get(String id) {
        return commentMapper.query(id);
    }

    @Override
    public List<CommentBean> getList(int offset, int limit, String bid, int status, String keyword) {
        JetCriteria criteria = new JetCriteria();
        criteria.setSelect("comment.*,user.name as userName,content.title as contentTitle");
        criteria.setUnion("user", "comment.uid=user.id", JetCriteria.JOIN_LEFT);
        criteria.setUnion("content", "comment.bid=content.id", JetCriteria.JOIN_LEFT);
        criteria.add("bid", bid);
        if (status != -1) {
            criteria.add("comment.status", status);
        }
        criteria.addLike("comment.content", keyword);
        criteria.setPage(offset, limit);
        criteria.orderBy("createTime", JetCriteria.ORDER_DESC);
        return commentMapper.queryList(criteria);
    }

    @Override
    public void update(CommentBean comment) {
        commentMapper.update(comment);
    }

    @Override
    public void delete(String id) {
        commentMapper.delete(id);
    }

}